﻿using System;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;

using Com.Ompchina.Lib.DBHelper;
using Model.AuthModel;
using Model.Base;
using IDAL.AuthModel;
namespace SQLServerDAL.AuthModel
{
    /// <summary>
    /// 通用组件，权限模型，数据层，功能
    /// </summary>
    public class FuncDAL : DalBase, IFunc
    {
        public FuncDAL() 
        {
            Schema = "am.";
            TableName = "Func";
            Fields = "FuncID, FuncName, FuncDescription, FuncMenu";
        }

        #region IFunc 成员
        /// <summary>
        /// 新增功能
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public bool InsertFunc(FuncInfo info)
        {
            SqlParameter[] parm = new SqlParameter[] { 
                new SqlParameter("@FuncName",SqlDbType.VarChar,20)
                ,new SqlParameter("@FuncDescription",SqlDbType.NVarChar,100)
                ,new SqlParameter("@FuncMenu",SqlDbType.Char,1)
                ,new SqlParameter("@RtnCode",SqlDbType.Char,2)
            };
            int i = 0;
            parm[i++].Value = info.FuncName;
            parm[i++].Value = info.FuncDescription;
            parm[i++].Value = info.FuncMenu;
            parm[i++].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(base.ConnectionString, CommandType.StoredProcedure, Schema + "AddFunc", parm);
            return parm[parm.Length - 1].Value.ToString().Equals("80");
        }

        /// <summary>
        /// 更新功能
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public bool UpdateFunc(FuncInfo info)
        {
            SqlParameter[] parm = new SqlParameter[] { 
                new SqlParameter("@FuncID",SqlDbType.Int)
                ,new SqlParameter("@FuncName",SqlDbType.VarChar,20)
                ,new SqlParameter("@FuncDescription",SqlDbType.NVarChar,100)
                ,new SqlParameter("@FuncMenu",SqlDbType.Char,1)
                ,new SqlParameter("@RtnCode",SqlDbType.Char,2)
            };
            int i = 0;
            parm[i++].Value = info.FuncID;
            parm[i++].Value = info.FuncName;
            parm[i++].Value = info.FuncDescription;
            parm[i++].Value = info.FuncMenu;
            parm[i++].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(base.ConnectionString, CommandType.StoredProcedure, Schema + "UpdateFunc", parm);
            return parm[parm.Length - 1].Value.ToString().Equals("80");
        }

        /// <summary>
        /// 删除功能
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteFunc(int id)
        {
            SqlParameter[] parm = new SqlParameter[] { 
                new SqlParameter("@FuncID",SqlDbType.Int)
                ,new SqlParameter("@RtnCode",SqlDbType.Char,2)
            };
            int i = 0;
            parm[i++].Value = id;
            parm[i++].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(base.ConnectionString, CommandType.StoredProcedure, Schema + "DeleteFunc", parm);
            return parm[parm.Length - 1].Value.ToString().Equals("80");
        }

        /// <summary>
        /// 根据编号获取功能
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public FuncInfo GetFuncByID(int id)
        {
            Sql = string.Format("SELECT TOP 1 {0} FROM {1} WHERE FuncID = {2}", Fields, Schema + TableName, id);
            SqlDataReader dr = SqlHelper.ExecuteReader(base.ConnectionString, CommandType.Text, Sql);
            FuncInfo info = new FuncInfo();
            if (dr != null)
            {
                if (dr.Read())
                {
                    int i = 0;
                    info.FuncID = dr.GetInt32(i++);
                    info.FuncName = dr.GetString(i++);
                    info.FuncDescription = dr.GetString(i++);
                    info.FuncMenu = dr.GetString(i++);                    
                }
                dr.Close();
            }
            return info;
        }

        /// <summary>
        /// 功能列表
        /// </summary>
        /// <returns></returns>
        public IList<FuncInfo> ListFunc(BaseCondition condition)
        {
            SqlBuilder = new StringBuilder("SELECT ");
            if (condition.Top > 0)
                SqlBuilder.AppendFormat(" TOP ({0}) ", condition.Top);
            SqlBuilder.AppendFormat(" {0} FROM {1} WHERE 1 = 1 ", Fields, Schema + TableName);
            if (!string.IsNullOrEmpty(condition.OrderBy))
                SqlBuilder.AppendFormat(" ORDER BY {0}", condition.OrderBy);
            List<FuncInfo> list = new List<FuncInfo>();
            SqlDataReader dr = SqlHelper.ExecuteReader(base.ConnectionString, CommandType.Text, SqlBuilder.ToString());
            if (dr != null)
            {
                while (dr.Read())
                {
                    FuncInfo info = new FuncInfo();
                    int i = 0;
                    info.FuncID = dr.GetInt32(i++);
                    info.FuncName = dr.GetString(i++);
                    info.FuncDescription = dr.GetString(i++);
                    info.FuncMenu = dr.GetString(i++);
                    list.Add(info);
                }
                dr.Close();
            }
            return list;
        }
        #endregion
    }
}
